package com.lqq.eduservice.controller.admin;


import com.lqq.commonutils.R;
import com.lqq.eduservice.entity.EduChapter;
import com.lqq.eduservice.entity.chapter.ChapterVo;
import com.lqq.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin//解决跨域
public class EduChapterController {

    @Autowired
    EduChapterService chapterService;

    //根据课程id查询课程大纲列表章节小节
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    //添加章节
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        chapterService.save(chapter);
        return R.ok();
    }

    //根据章节id查询信息
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }

    //修改章节
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    //删除章节
    @DeleteMapping("/delete/{chapterId}")
    public R delete(@PathVariable String chapterId) {
        Boolean result = chapterService.deleteChapter(chapterId);
        if (result) {
            return R.ok();
        }
        else {
            return R.error();
        }
    }
}

